export var MyCalendar = function () {
  this.books = [] // 把所有book按顺序放入数组
}

/**
 * @param {number} start
 * @param {number} end
 * @return {boolean}
 */
MyCalendar.prototype.book = function (start, end) {
  if (this.books.length === 0) {
    this.books.push([start, end])
    return true
  }
  // 通过二分查找找插入点
  let [left, right] = [0, this.books.length - 1]
  while (left <= right) {
    let mid = (left + right) >> 1
    if (this.books[mid][0] >= end) {
      right = mid - 1
    } else if (this.books[mid][1] <= start) {
      left = mid + 1
    } else {
      return false
    }
  }
  this.books.splice(left, 0, [start, end])
  return true
}

/**
 * Your MyCalendar object will be instantiated and called as such:
 * var obj = new MyCalendar()
 * var param_1 = obj.book(start,end)
 */
